<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:util="http://exist-db.org/xquery/util"
    xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb"
    xmlns:xu="http://www.xmldb.org/xupdate" xmlns:xforms="http://www.w3.org/2002/xforms">

	<p:param type="input" name="instance"/>
	<p:param type="output" name="data"/>

	<!--read global parameters-->
	<p:processor name="oxf:identity">
		<p:input name="data" href="/config/config.xml"/>
		<p:output name="data" id="global_pref"/>
	</p:processor>

	<!--load translation-->
	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#global_pref#xpointer(/globalpref/lang)"/>
		<p:input name="config" href="/apps/engine/utils/XMLtranslation.xpl"/>
		<p:output name="data" id="translation"/>
	</p:processor>

	<p:choose href="#instance">
		<p:when test="not(exists(/null))">
			<p:processor name="oxf:xforms-submission">
				<p:input name="submission">
					<xforms:submission method="get" action="/exist/rest/db/portal/users.xml"/>
				</p:input>
				<p:input name="request">
					<dummy/>
				</p:input>
				<p:output name="response" id="response"/>
			</p:processor>

			<p:processor name="oxf:unsafe-xslt">
				<p:input name="data" href="#response"/>
				<p:input name="submission" href="#instance"/>
				<p:input name="config">
					<error xsl:version="2.0">
						<xsl:variable name="id" select="doc('input:submission')/user/username"/>
						<xsl:value-of select="count(/users/user[@id= $id]/username)"/>
					</error>
				</p:input>
				<p:output name="data" id="existence"/>
			</p:processor>

			<p:processor name="oxf:xslt">
				<p:input name="data" href="#instance"/>
				<p:output name="data" id="insertion"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
						<xsl:text>xquery version "1.0";</xsl:text>
						update insert
						<user>
							<xsl:attribute name="id">
								<xsl:value-of select="/user/username"/>
							</xsl:attribute>
							<username>
								<xsl:value-of select="/user/username"/>
							</username>
							<password>{util:md5(string('<xsl:value-of select="/user/password"/>'))}</password>
							<copname/>
							<level>0</level>
							<lang>en</lang>
							<interface/>
						</user> into /users
					</xdb:query>
				</p:input>
			</p:processor>

			<p:choose href="#existence">
				<p:when test="not(/error > 0)">
					<p:processor name="oxf:xmldb-query">
						<p:input name="datasource" href="/config/datasource.xml"/>
						<p:input name="query" href="#insertion"/>
						<p:output name="data" id="dummy"/>
					</p:processor>
					<p:processor name="oxf:null-serializer">
						<p:input name="data" href="#dummy"/>
					</p:processor>
					<p:processor name="oxf:identity">
						<p:input name="data">
							<message>The user has been successfuly created.</message>
						</p:input>
						<p:output name="data" id="error"/>
					</p:processor>
					<p:processor name="oxf:redirect">
						<p:input name="data">
							<redirect-url>
								<path-info>/login</path-info>
								<parameters>
									<parameter>
										<name>success</name>
										<value>1</value>
									</parameter>
								</parameters>
							</redirect-url>
						</p:input>
					</p:processor>
				</p:when>
				<p:otherwise>
					<p:processor name="oxf:identity">
						<p:input name="data">
							<error>Username already in use.</error>
						</p:input>
						<p:output name="data" id="error"/>
					</p:processor>
				</p:otherwise>
			</p:choose>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:identity">
				<p:input name="data">
					<error/>
				</p:input>
				<p:output name="data" id="error"/>
			</p:processor>
		</p:otherwise>
	</p:choose>
	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #global_pref#xpointer(/globalpref/appname),#translation, #error)"/>
		<p:output name="data" ref="data"/>
	</p:processor>
</p:config>